package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Spectral;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.exception.FileException;
import com.jgsu.farm.service.SpectralService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Slf4j
@Api(value = "farm/sysuser/spectral", tags = "光谱信息管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/spectral")
public class SpectralController {
    @Autowired
    SpectralService spectralService;

    /**
     * 光谱信息管理API，待扩展。。。
     *
     * */


    /**
     * 光谱信息添加
    @ApiOperation(value = "光谱信息添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult spectralAdd(Spectral spectral,
                                    @PathVariable(value = "file", required = false) MultipartFile file) throws IOException {
        try {
            // 实现文件上传
            DownLoadFiles downLoadFiles = new DownLoadFiles();
            String filepath = downLoadFiles.DownLoadPhoto(file);
            spectral.setSpectralfile(filepath);
            Date date = new Date(System.currentTimeMillis());
            spectral.setTime(date);
            // 4.将spectral表的信息插入进去
            spectralService.add(spectral.getName(), spectral.getFormat(), spectral.getDescription(), spectral.getAuthor(),
                    date, spectral.getLocation(), spectral.getDevice(), filepath);
            log.info("光谱信息添加，光谱名字为：" + spectral.getName());
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException da) {
            log.error("资讯信息添加异常,名字为：" + spectral.getName());
            throw new DataNotExistException(da.getCode(), da.getMessage());
        } catch (FileException fe) {
            throw new FileException(fe.getCode(), fe.getMessage());
        } catch (IOException e) {
            throw new IOException("IO异常");
        }
    }

    *//**
     * 光谱信息删除
     *//*
    @ApiOperation(value = "光谱信息删除功能")
    @PostMapping(value = "/delete", produces = "application/json")
    public CommonResult spectralDelete(@RequestParam("spectralid") Integer spectralid) {
        try {
            spectralService.delete(spectralid);
            log.info("删除id为：的光谱信息", spectralid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("不存在id为：{}的光谱信息，删除失败", spectralid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    *//**
     * 光谱信息查询功能
     *//*
    @ApiOperation(value = "光谱信息查询功能")
    @PostMapping(produces = "application/json")
    public CommonResult<PageInfo<Spectral>> spectralList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                         @RequestParam(value = "pagesize", required = false, defaultValue = "3") int pagesize) {
        try {
            PageInfo<Spectral> spectrals = spectralService.selectAll(page, pagesize);
            log.info("查询所有的光谱信息");
            return CommonResult.success(spectrals);
        } catch (DataNotExistException e) {
            log.error("光谱信息表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    *//**
     * 光谱信息修改
     *//*
    @ApiOperation(value = "光谱信息修改功能")
    @PostMapping(value = "/update", produces = "application/json")
    public CommonResult spectralUpdate(Spectral spectral) {
        try {
            Integer modify = spectralService.modify(spectral);
            log.info("光谱信息更新，更新id为：{}", spectral.getSpectralid());
            return CommonResult.success(modify);
        } catch (DataNotExistException e) {
            log.error("更新id = {}的光谱信息失败", spectral.getSpectralid());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }*/
}
